package com.bf.xpdd.persistent.sqlmapdao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.bf.xpdd.domain.ActivityItemBean;
import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * 活动商品DAO实现
 * 
 * @author linmu
 * 
 */
public class DaoActivityItemImpl implements com.bf.xpdd.persistent.iface.DaoActivityItem
{
	/**
	 * 日志控制器
	 */
	private Logger logger = Logger.getLogger(DaoActivityItemImpl.class);

	/**
	 * ibatis的查询接口
	 */
	private SqlMapClient sqlMapClient;

	/**
	 * 注入sqlMapClient
	 * 
	 * @param sqlMapClient
	 */
	public void setSqlMapClient(SqlMapClient sqlMapClient)
	{
		this.sqlMapClient = sqlMapClient;
	}

	@Override
	public void addActivityItem(ActivityItemBean item)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.insert("activityItem.addActivityItemBean", item);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public void addActivityItemBatch(List<ActivityItemBean> itemList)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			// 备份事务提交类型
			boolean flag = this.sqlMapClient.getCurrentConnection().getAutoCommit();
			// 设置自动提交不可用
			this.sqlMapClient.getCurrentConnection().setAutoCommit(false);
			// 开启批量状态
			this.sqlMapClient.startBatch();

			for (ActivityItemBean bean : itemList)
			{
				this.sqlMapClient.insert("activityItem.addActivityItemBean", bean);
			}

			this.sqlMapClient.executeBatch();
			this.sqlMapClient.commitTransaction();
			// 重设事务提交类型
			this.sqlMapClient.getCurrentConnection().setAutoCommit(flag);
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public ActivityItemBean queryActivityItemById(long id)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			List<ActivityItemBean> list = (List<ActivityItemBean>) this.sqlMapClient.queryForList(
					"activityItem.queryActivityItemBeanById", id);
			this.sqlMapClient.commitTransaction();
			if (list.size() > 0)
			{
				return list.get(0);
			}
			return null;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public List<ActivityItemBean> queryActivityItemByActId(long actId)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			List<ActivityItemBean> list = (List<ActivityItemBean>) this.sqlMapClient.queryForList(
					"activityItem.queryActivityItemBeanByActId", actId);
			this.sqlMapClient.commitTransaction();
			return list;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public void delActivityItemBeanById(long id)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.delete("activityItem.delActivityItemBeanById", id);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public void delActivityItemBeanByActId(long id)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.delete("activityItem.delActivityItemBeanByActId", id);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public List<Long> queryActItemIdsByActId(long actId)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			List<Long> list = (List<Long>) this.sqlMapClient.queryForList(
					"activityItem.queryActItemIdsByActId", actId);
			this.sqlMapClient.commitTransaction();
			return list;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public void delActivityItemByIdBatch(long aid, List<Long> itemIdList)
	{
		if (itemIdList.size() < 0)
		{
			logger.debug("del itemid is null");
			return;
		}
		
		try
		{
			this.sqlMapClient.startTransaction();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("actId", aid);
			
			StringBuffer buf = new StringBuffer();
			for (Long id : itemIdList)
			{
				buf.append(id.longValue()).append(",");
			}
			String idStr = buf.substring(0, buf.length() - 1); 
			map.put("itemId", idStr);
			this.sqlMapClient.delete("activityItem.delActivityItemByIdBatch", map);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public List<ActivityItemBean> queryActItemByActIds(String actIds)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			List<ActivityItemBean> list = (List<ActivityItemBean>) this.sqlMapClient.queryForList(
					"activityItem.queryActItemByActIds", actIds);
			this.sqlMapClient.commitTransaction();
			return list;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public List<Long> queryActItemIdsByNick(String nick, long actId)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("nick", nick);
		map.put("actId", actId);
		try
		{
			this.sqlMapClient.startTransaction();
			List<Long> list = (List<Long>) this.sqlMapClient.queryForList(
					"activityItem.queryActItemIdsByNick", map);
			this.sqlMapClient.commitTransaction();
			return list;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}
}
